<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\SupplieLogic;
use App\Http\Resources\SupplyResource;
use App\Model\OASupplier;
use Illuminate\Http\Request;

class SupplierController extends Controller
{
    public function registerSupplier(Request $request)
    {
        try{
            $rules = [
                'company'=>'required|string',
                'username'=>'required|string',
                'address'=>'required|string',
                'email'=>'required|string',
                'account'=>'required|string',
                'phone'=>'required|string',
                'password'=>'required|string',
                'repeatPassword'=>'required|string',
            ];
            $params = $request->all();
            $this->verify($params,$rules);
            $result = (new SupplieLogic())->registerSupplier($params);
            ret_json('ok','注册成功',$result);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    public function getProjectList()
    {
        try{
            $list = (new SupplieLogic())->getProjectList();
            ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //添加订单
    public function addOrder(Request $request)
    {
        try{

            $rules = [
                'xmid'=>'required|numeric',
                'start_at'=>'required|string',
                'end_at'=>'required|string',
                'limit_money'=>'required|string',
                'reduce_money'=>'required|string',
                'rows'=>'required|numeric',
                'files'=>'sometimes|array',
                'order_id'=>'sometimes|nullable',
            ];
           $this->verify($request->all(),$rules);
            $res = (new SupplieLogic())->addOrder($request->all());
            ret_json('ok',$res['msg'],$res);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    public function getSupplyList(Request $request)
    {

       
       $rules = [
        'status'=>'sometimes|numeric|in:0,1,2|nullable',
        'company'=>'sometimes|string|nullable',
        'account'=>'sometimes|string|nullable',
        'page'=>'required|numeric|min:1',
        'page_size'=>'required|numeric|min:10'
       ];
       $this->verify($request->all(),$rules);

        //dd('1111');

        $data = (new SupplieLogic())->getSupplyList($request);

        //$data->load('total');

        return $this->SucRes(SupplyResource::collection($data));
    }

    public function editSupply(Request $request)
    {
      $rules = [
        'id'=>'required|exists:supplier',
        'status'=>"required|in:1,2",
        'reason'=>'sometimes|string'
      ];
      $this->verify($request->all(),$rules);
       $result = (new SupplieLogic())->editSupply($request);

       ret_json('ok','操作成功',['id'=>$result]);
    }

    public function delSupply(Request $request)
    {
       $rules = [
        'id'=>'required|exists:supplier',
       ];
       $this->verify($request->all(),$rules);

     OASupplier::where('id',$request->id)->delete();

    return $this->SucRes(['id'=>$request->id]);
    }
}
